package com.zking.ssm.controller;


import com.zking.ssm.model.SysRoler;
import com.zking.ssm.model.SysUser;
import com.zking.ssm.model.SysUserRoler;
import com.zking.ssm.service.ISysRolerService;
import com.zking.ssm.service.ISysUserRolerService;
import com.zking.ssm.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Role")
public class SysRoleController {

    @Autowired
    private ISysRolerService irole;

    @Autowired
    private ISysUserRolerService isur;


    @RequestMapping("/add")
    public Map<String,Object> add(SysRoler sysRoler){
        int n = irole.insert(sysRoler);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","增加角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","增加角色失败！");
        }
        return map;
    }


    @RequestMapping("/del")
    public Map<String,Object> del(SysRoler sysRoler){
        int n = irole.deleteByPrimaryKey(sysRoler.getRoleId());
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","删除角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","删除角色失败！");
        }
        return map;
    }

    @RequestMapping("/update")
    public Map<String,Object> update(SysRoler sysRoler){
        int n = irole.updateByPrimaryKeySelective(sysRoler);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","修改角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","修改角色失败！");
        }
        return map;
    }


    @RequestMapping("/list")
    public Map<String,Object> list(SysRoler sysRoler, HttpServletRequest request){
        PageBean pageBean = new PageBean();
        //初始化pagebean
        pageBean.setRequest(request);
        System.out.println(pageBean);
        List<SysRoler> list = irole.selectAll(sysRoler,pageBean);
        System.out.println(pageBean);


        Map<String,Object> map = new HashMap<>();
        if(list.size() > 0){
            map.put("code",0);
            map.put("result",list);
            map.put("message","查询角色成功！");
            map.put("page",pageBean.getPage());//设置分页页码
            map.put("rows",pageBean.getRows());//设置分页页大小
            map.put("total",pageBean.getTotal());//设置分页数据总条数
        }else{
            map.put("code",-1);
            map.put("message","查询角色失败！");
        }
        return map;
    }



    @RequestMapping("/toUserRemove")
    public Map<String,Object> toUserRemove(SysUserRoler sysUserRoler){
        int n = isur.toUserRemove(sysUserRoler);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","撤销用户角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","撤销用户角色失败！");
        }
        return map;
    }

    @RequestMapping("/addRoleToUser")
    public Map<String,Object> addRoleToUser(SysUserRoler sysUserRoler){
        System.out.println("前端传过来的数据"+sysUserRoler);
        SysRoler sysRoler = new SysRoler();
        sysRoler.setUserId(sysUserRoler.getUserId());
        System.out.println("前端传过来的数据"+sysRoler);
        SysRoler roler = irole.selectRoleId(sysRoler);
        System.out.println("用户的角色是"+roler);
        Map<String,Object> map = new HashMap<>();
        if(roler != null){
            map.put("message","用户已被授予过角色，请勿重复授予！");
        }else{
            int n = isur.addRoleToUser(sysUserRoler);
            if(n > 0){
                map.put("code",0);
                map.put("message","给用户授予角色成功！");
            }else{
                map.put("code",-1);
                map.put("message","给用户授予角色失败！");
            }
        }
        return map;
    }

    @RequestMapping("/upUserRole")
    public Map<String,Object> upUserRole(SysUserRoler sysUserRoler){
        int n = isur.updateByPrimaryKeySelective(sysUserRoler);
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","修改用户角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","修改用户角色失败！");
        }
        return map;
    }

    @RequestMapping("/selectUserRole")
    public Map<String,Object> selectUserRole(SysRoler sysRoler){//查询用户拥有角色
        PageBean pageBean = new PageBean();
        SysRoler role = irole.selectRoleId(sysRoler);
        List<SysRoler> list = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        if(role != null){
            list.add(role);
            map.put("code",0);
            map.put("result",list);
            map.put("message","查询用户角色成功！");
        }else{
            map.put("code",-1);
            map.put("message","查询用户角色失败！");
        }
        return map;
    }


    @RequestMapping("/noSelectUserRole")
    public Map<String,Object> noSelectUserRole(SysRoler sysRoler){//查询用户未拥有角色
        PageBean pageBean = new PageBean();
        List<SysRoler> sysRolers = irole.noSelectRole(sysRoler);
        Map<String,Object> map = new HashMap<>();
        if(sysRolers.size() != 0){
            map.put("code",0);
            map.put("result",sysRolers);
            map.put("message","查询用户未拥有角色！");
        }else{
            List<SysRoler> list = irole.selectAll(sysRoler,null);
            map.put("result",list);
            map.put("code",-1);
            map.put("message","查询用户未拥有角色！");
        }
        return map;
    }


}
